package com.three.lab.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.three.lab.MyApplication;
import com.three.lab.R;
import com.three.lab.util.LanguageUtils;
import com.three.lab.util.ToastUtils;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener{
    private View mContextView = null;
    protected InputMethodManager inputMethodManager;
    /** 是否允全透明**/
    protected boolean isTransparent = false;
    private LoadingPopupView loadingView = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            MyApplication.getInstances().pushTask(this);
            inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            Bundle bundle = getIntent().getExtras();
            initParms(bundle);
            mContextView = LayoutInflater.from(this)
                    .inflate(bindLayout(), null);
            setContentView(mContextView);
            loadingView = (LoadingPopupView) new XPopup.Builder(this)
                    .asLoading("正在初始化...");
            if (isTransparent) {
                setTransparent();
            } else {
                setStatusBar(R.color.app_titlebar_cor);
            }
            initViews(mContextView,savedInstanceState);
            setListener();
            initData(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void showLoading(String text){
        if (loadingView != null){
            loadingView.setTitle(text);
            loadingView.show();
        }
    }

    protected void hideLoading(){
        if (loadingView != null){
            loadingView.smartDismiss();
        }
    }

    public void setTransparent() {
        UltimateBarX.statusBarOnly(this)
                .transparent()
                .apply();
    }

    protected abstract void initParms(Bundle bundle);

    protected abstract int bindLayout();

    protected void setTransparent(boolean transparent) {
        this.isTransparent = transparent;
    }

    protected void setStatusBar(int color) {
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(color)
                .light(true)
                .apply();
    };

    protected abstract void initViews(final View view,Bundle savedInstanceState);

    protected abstract void setListener();

    protected abstract void widgetClick(View v);

    protected abstract void initData(Context mContext);

    @Override
    public void onClick(View v) {
        if (fastClick())
            widgetClick(v);
    }

    private boolean fastClick() {
        long lastClick = 0;
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }

    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int resId) {
        return (T) super.findViewById(resId);
    }

    protected void showToast(String msg){
        ToastUtils.showToast(msg);
    }

    protected long getTimestamp() {
        return MyApplication.getTimesTamp();
    }

    protected void hideSoftKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtils.setLocal(newBase));
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstances().removeTask(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
